import { createSlice, PayloadAction } from '@reduxjs/toolkit';

// 定义 InterestTag 类型
export interface InterestTag {
    id: number; // 或者 string，取决于后端数据库定义
    name: string;
    // 可能还有其他属性，如创建时间等，根据后端模型补充
}

export interface User {
    id: number;
    username: string;
    email: string;
    avatar?: string;
    bio?: string;
    gender?: string;
    age?: number;
    birthDate?: string;
    zodiacSign?: string;
    location?: string;
    followingCount?: number;
    favoritesCount?: number;
    points?: number;
    interestTags?: InterestTag[]; // 修改回 InterestTag[] 类型
}

interface UserState {
    isAuthenticated: boolean;
    user: User | null;
    loading: boolean;
    error: string | null;
}

const initialState: UserState = {
    isAuthenticated: false,
    user: null,
    loading: false,
    error: null,
};

const userSlice = createSlice({
    name: 'user',
    initialState,
    reducers: {
        loginStart: (state) => {
            state.loading = true;
            state.error = null;
        },
        loginSuccess: (state, action: PayloadAction<User>) => {
            state.isAuthenticated = true;
            state.user = action.payload;
            state.loading = false;
            state.error = null;
        },
        loginFailure: (state, action: PayloadAction<string>) => {
            state.loading = false;
            state.error = action.payload;
        },
        logout: (state) => {
            state.isAuthenticated = false;
            state.user = null;
            state.error = null;
        },
        updateProfile: (state, action: PayloadAction<Partial<User>>) => {
            if (state.user) {
                state.user = { ...state.user, ...action.payload };
            }
        },
        setAuthState: (state, action: PayloadAction<boolean>) => {
            state.isAuthenticated = action.payload;
        }
    },
});

export const {
    loginStart,
    loginSuccess,
    loginFailure,
    logout,
    updateProfile,
    setAuthState
} = userSlice.actions;

export default userSlice.reducer; 